{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 11_Train_final_model_ComplEx\n",
    "#\n",
    "# created by LuYF-Lemon-love <luyanfeng_nlp@qq.com> on March 11, 2023\n",
    "# updated by LuYF-Lemon-love <luyanfeng_nlp@qq.com> on March 11, 2023\n",
    "#\n",
    "# 该脚本展示了如何在 DRKG 上训练最终模型 (ComplEx).\n",
    "#\n",
    "# 需要的包:\n",
    "#          torch\n",
    "#          dgl, version: 0.4.3\n",
    "#          dglke\n",
    "#\n",
    "# 需要的文件:\n",
    "#          ../../data/drkg/drkg.tsv\n",
    "#\n",
    "# 源教程链接: https://github.com/gnn4dr/DRKG/blob/master/embedding_analysis/Train_embeddings.ipynb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Training DRKG Using ComplEx\n",
    "\n",
    "这个 notebook 展示了如何在 DRKG 上训练最终模型 (ComplEx)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们能使用 DGL-KE 命令训练 ComplEx 模型, 关于如何使用 DGL-KE 的更多信息请参考 https://github.com/awslabs/dgl-ke.\n",
    "\n",
    "这里我们使用两个 GPU 训练模型."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- batch_size: **4096**\n",
    "\n",
    "- neg_sample_size: **256**\n",
    "\n",
    "- hidden_dim: 200, **400**\n",
    "\n",
    "- gamma: **50**, 125, 200\n",
    "\n",
    "- lr: 0.01, 0.05, **0.1**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reading train triples....\n",
      "Finished. Read 5874261 train triples.\n",
      "|Train|: 5874261\n",
      "random partition 5874261 edges into 2 parts\n",
      "part 0 has 2937131 edges\n",
      "part 1 has 2937130 edges\n",
      "/home/luyanfeng/miniconda3/envs/drkg/lib/python3.8/site-packages/dgl/base.py:25: UserWarning: multigraph will be deprecated.DGL will treat all graphs as multigraph in the future.\n",
      "  warnings.warn(msg, warn_type)\n",
      "Total initialize time 14.694 seconds\n",
      "[proc 1][Train](20000/100000) average pos_loss: 0.3357126424908638\n",
      "[proc 0][Train](20000/100000) average pos_loss: 0.3354531366929412\n",
      "[proc 1][Train](20000/100000) average neg_loss: 0.4856368833363056\n",
      "[proc 0][Train](20000/100000) average neg_loss: 0.4853328426361084\n",
      "[proc 1][Train](20000/100000) average loss: 0.4106747629150748\n",
      "[proc 0][Train](20000/100000) average loss: 0.4103929898366332\n",
      "[proc 1][Train](20000/100000) average regularization: 0.02906323681493668\n",
      "[proc 0][Train](20000/100000) average regularization: 0.02911832661129738\n",
      "[proc 1][Train] 20000 steps take 543.714 seconds\n",
      "[proc 1]sample: 67.906, forward: 252.778, backward: 71.159, update: 140.916\n",
      "[proc 0][Train] 20000 steps take 543.724 seconds\n",
      "[proc 0]sample: 71.539, forward: 262.961, backward: 73.651, update: 135.273\n",
      "[proc 1][Train](40000/100000) average pos_loss: 0.3119826398983598\n",
      "[proc 0][Train](40000/100000) average pos_loss: 0.3117216911017895\n",
      "[proc 1][Train](40000/100000) average neg_loss: 0.44923646594285965\n",
      "[proc 0][Train](40000/100000) average neg_loss: 0.4488734394714236\n",
      "[proc 1][Train](40000/100000) average loss: 0.38060955295711757\n",
      "[proc 1][Train](40000/100000) average regularization: 0.03354529978763312\n",
      "[proc 0][Train](40000/100000) average loss: 0.38029756531864406\n",
      "[proc 1][Train] 20000 steps take 547.330 seconds\n",
      "[proc 1]sample: 68.787, forward: 258.177, backward: 71.505, update: 143.112\n",
      "[proc 0][Train](40000/100000) average regularization: 0.033566380400024354\n",
      "[proc 0][Train] 20000 steps take 547.332 seconds\n",
      "[proc 0]sample: 69.087, forward: 266.394, backward: 72.949, update: 138.141\n",
      "[proc 0][Train](60000/100000) average pos_loss: 0.3075039648383856\n",
      "[proc 1][Train](60000/100000) average pos_loss: 0.3077817863315344\n",
      "[proc 0][Train](60000/100000) average neg_loss: 0.4438560186907649\n",
      "[proc 1][Train](60000/100000) average neg_loss: 0.443975687970221\n",
      "[proc 0][Train](60000/100000) average loss: 0.3756799916952848\n",
      "[proc 1][Train](60000/100000) average loss: 0.37587873724401\n",
      "[proc 0][Train](60000/100000) average regularization: 0.03487160711046308\n",
      "[proc 1][Train](60000/100000) average regularization: 0.03485194835271686\n",
      "[proc 0][Train] 20000 steps take 538.498 seconds\n",
      "[proc 0]sample: 70.110, forward: 254.889, backward: 74.598, update: 135.488\n",
      "[proc 1][Train] 20000 steps take 538.504 seconds\n",
      "[proc 1]sample: 69.699, forward: 253.608, backward: 73.711, update: 137.504\n",
      "[proc 0][Train](80000/100000) average pos_loss: 0.30533153931349516\n",
      "[proc 1][Train](80000/100000) average pos_loss: 0.3055799445658922\n",
      "[proc 0][Train](80000/100000) average neg_loss: 0.4412454686522484\n",
      "[proc 0][Train](80000/100000) average loss: 0.3732885039806366\n",
      "[proc 1][Train](80000/100000) average neg_loss: 0.4413009587466717\n",
      "[proc 0][Train](80000/100000) average regularization: 0.0356366937590763\n",
      "[proc 1][Train](80000/100000) average loss: 0.3734404516145587\n",
      "[proc 0][Train] 20000 steps take 534.552 seconds\n",
      "[proc 0]sample: 70.554, forward: 250.378, backward: 73.878, update: 138.081\n",
      "[proc 1][Train](80000/100000) average regularization: 0.03561777354385704\n",
      "[proc 1][Train] 20000 steps take 534.561 seconds\n",
      "[proc 1]sample: 68.846, forward: 248.501, backward: 73.017, update: 136.532\n",
      "[proc 1][Train](100000/100000) average pos_loss: 0.30427735563218594\n",
      "[proc 0][Train](100000/100000) average pos_loss: 0.3036940054535866\n",
      "[proc 1][Train](100000/100000) average neg_loss: 0.439355016797781\n",
      "[proc 1][Train](100000/100000) average loss: 0.37181618622243406\n",
      "[proc 0][Train](100000/100000) average neg_loss: 0.43933770617246626\n",
      "[proc 1][Train](100000/100000) average regularization: 0.03613581902813166\n",
      "[proc 1][Train] 20000 steps take 624.126 seconds\n",
      "[proc 1]sample: 68.788, forward: 240.251, backward: 74.172, update: 238.844\n",
      "proc 1 takes 2788.237 seconds\n",
      "[proc 0][Train](100000/100000) average loss: 0.3715158558115363\n",
      "[proc 0][Train](100000/100000) average regularization: 0.03615506561342627\n",
      "[proc 0][Train] 20000 steps take 624.144 seconds\n",
      "[proc 0]sample: 67.956, forward: 246.181, backward: 71.846, update: 130.786\n",
      "proc 0 takes 2788.250 seconds\n",
      "training takes 2788.6544847488403 seconds\n",
      "Save model to ckpts/ComplEx_All_DRKG_0\n"
     ]
    }
   ],
   "source": [
    "!DGLBACKEND=pytorch dglke_train --dataset All_DRKG --data_path ../../data/drkg \\\n",
    "--data_files drkg.tsv --format 'raw_udd_hrt' \\\n",
    "--model_name ComplEx \\\n",
    "--batch_size 4096 --neg_sample_size 256 --hidden_dim 400 \\\n",
    "--gamma 50.0 --lr 0.1 --max_step 100000 -adv --regularization_coef 1.00E-07 \\\n",
    "--gpu 0 1 --num_proc 2 --mix_cpu_gpu --async_update --force_sync_interval 1000 \\\n",
    "--log_interval 20000 --num_thread 32"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  },
  "vscode": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
